package arrray;

/**
 * @author huangxianjin
 * @date 2025/8/20 23:32
 * @description "接雨水"
 */
public class LC_42 {
    /*
    * 思想：盛雨水的多少取决于最短长度
    * */
    //官方做法-双指针
    public int trap(int[] height) {
        if (height == null || height.length == 0) {
            return 0;
        }
        int left = 0;
        int right = height.length - 1;

        int leftMax = 0;
        int rightMax = 0;

        int sum = 0;
        while (left < right) {
            if (height[left] < height[right]) {
                leftMax = Math.max(height[left], leftMax);
                sum += leftMax - height[left];
                ++left;
            } else {
                rightMax = Math.max(height[right], rightMax);
                sum += rightMax - height[right];
                --right;
            }
        }
        return sum;
    }
}
